package cn.wp.ewallet.wmps.dao.impl;

import cn.wp.common.core.dao.SqlString;
import cn.wp.ewallet.wmps.dao.ProdOrderRelDao;
import cn.wp.ewallet.wmps.extension.constant.EwalletWmpsConstant;
import cn.wp.ewallet.wmps.model.entity.ProdOrderRelDo;
import cn.wp.ewallet.wmps.model.entity.PurchasedProdOrderRelDoo;
import org.springframework.stereotype.Repository;
import cn.wp.common.core.dao.JdbcSupportTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

/**
 * 理财订单-数据访问层实现
 * Created by zhangyongli on 2020/01/08
 */
@Repository
public class ProdOrderRelDaoImpl implements ProdOrderRelDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public List<ProdOrderRelDo> selectProdOrderRelByAccountId(String accountId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from prod_order_rel");
        sqlString.add("where account_id = ?");
        sqlString.add("and (ORDER_STATUS = ? or ORDER_STATUS = ?)");

        return jdbcSupportTemplate.selectList(sqlString.build(), new Object[]{accountId, EwalletWmpsConstant.ORDER_STATUS_EFFECTIVE, EwalletWmpsConstant.ORDER_STATUS_WAITING}, ProdOrderRelDo.class);

    }

    @Override
    public BigDecimal selectFrozenAmount(String accountId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select count(purch_amount)");
        sqlString.add("from prod_order_rel");
        sqlString.add("where account_id = ?");
        sqlString.add("    and order_status in (?, ?)");
        return jdbcSupportTemplate.selectValue(sqlString.build(),
                new Object[]{
                        accountId,
                        EwalletWmpsConstant.ORDER_STATUS_BEARING,
                        EwalletWmpsConstant.ORDER_STATUS_WAITING
                }, BigDecimal.class);
    }

    @Override
    public void insertProdOrderRel(ProdOrderRelDo prodOrderRelDo) {
        jdbcSupportTemplate.insert(prodOrderRelDo);
    }

    @Override
    public ProdOrderRelDo selectProdOrderRelByProdOrderId(String prodOrderId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("SELECT *");
        sqlString.add("FROM PROD_ORDER_REL");
        sqlString.add("WHERE PROD_ORDER_ID = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{prodOrderId},ProdOrderRelDo.class);
    }

    @Override
    public void updateProdOrderRelDo(ProdOrderRelDo prodOrderRelDo) {
        jdbcSupportTemplate.updateSelective(prodOrderRelDo);

    }

    @Override
    public List<ProdOrderRelDo> selectProdOrderRels() {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from prod_order_rel a, FINA_PROD_INFO b");
        sqlString.add("where a.FINA_PROD_ID = b.FINA_PROD_ID");
        sqlString.add("    and a.ORDER_STATUS = ? and b.PROD_TYPE=?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{EwalletWmpsConstant.ORDER_STATUS_BEARING,EwalletWmpsConstant.PROD_TYPE_REGULAR},ProdOrderRelDo.class);
    }

    @Override
    public List<ProdOrderRelDo> selectProdOrderRelListByAccountIdAndOrderStatus(String accountId, String orderStatus) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select p.* from prod_order_rel p where p.account_id = ? and p.order_status = ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{accountId,orderStatus},ProdOrderRelDo.class);
    }

    @Override
    public List<ProdOrderRelDo> selectListByOrderStatus(String orderStatus) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select p.* from prod_order_rel p where p.order_status = ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{orderStatus},ProdOrderRelDo.class);
    }

    @Override
    public PurchasedProdOrderRelDoo selectProdOrderRelDetailByProdOrderRelId(String prodOrderRelId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select r.PROD_ORDER_ID, f.fina_prod_name, r.PURCH_TIME, r.EFF_DATE, x.EXP_TIME, fp.FREE_PERIOD");
        sqlString.add("    , r.purch_amount, x.IS_AUTO_RENEWAL, f.fina_prod_id");
        sqlString.add("from prod_order_rel r, fina_prod_info f, FIX_PROD_ORDER_REL x, FIX_FINA_PROD_INFO fp");
        sqlString.add("where r.fina_prod_id = f.fina_prod_id");
        sqlString.add("    and r.prod_order_id = x.prod_order_id");
        sqlString.add("    and f.fina_prod_id = fp.fina_prod_id");
        sqlString.add("    and r.prod_order_id = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[]{prodOrderRelId},PurchasedProdOrderRelDoo.class);
    }

    @Override
    public BigDecimal selectSumPurchaseYeldByAccountAndType(String accountId, String prodTypeRegular) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select sum(a.PURCH_AMOUNT)");
        sqlString.add("from prod_order_rel a, fina_prod_info b");
        sqlString.add("where a.FINA_PROD_ID = b.FINA_PROD_ID");
        sqlString.add("    and b.PROD_TYPE = ?");
        sqlString.add("    and a.ACCOUNT_ID = ?");
        return jdbcSupportTemplate.selectValue(sqlString.build(),new Object[]{prodTypeRegular,accountId},BigDecimal.class);
    }

}

